<?php

/**
 * ECSHOP 自助印刷
*/

define('IN_ECS', true);

require(dirname(__FILE__) . '/includes/init.php');
if ((DEBUG_MODE & 2) != 2)
{
    $smarty->caching = true;
}

/*------------------------------------------------------ */
//-- 判断是否存在缓存，如果存在则调用缓存，反之读取相应内容
/*------------------------------------------------------ */
/* 缓存编号 */
$cache_id = sprintf('%X', crc32('zzys'.'-'.$_SESSION['user_rank'] . '-' . $_CFG['lang']));

if (!$smarty->is_cached('zzys.dwt', $cache_id))
{
    assign_template();

    $smarty->assign('keywords',        '自助印刷');
    $smarty->assign('description',     '自助印刷');

    $smarty->assign('categories',       get_categories_tree()); // 分类树
	$smarty->assign('goods2_list',     get_recommend_goods2_list('best'));  // 精品稿件（稿件推荐）
	$cid = 0;
	$categories = get_categories_tree();
	foreach($categories as $cat){
		foreach($cat['cat_id'] as $catt){
			if($cid == 0){
			$cid = $catt['id'];
			}
		}
	}
//print_r(get_recommend_goods2_list('best'));exit;
	$smarty->assign('cid',     $cid);
	$smarty->assign('menu_index',		'5');//当前导航标识

    /* 页面中的动态内容 */
    assign_dynamic('zzys');
}

$smarty->display('zzys.dwt', $cache_id);

//获取精品稿件（稿件推荐）、最新稿件、热销稿件 
function get_recommend_goods2_list( $type , $num = 10){
    $where = " g.is_delete = 0 AND g.type = 1 ";

    if ('best' == $type )
    {
        $where .= " AND g.is_best = 1 ";
    }
	else if ('new' == $type )
    {
        $where .= " AND g.is_new = 1 ";
    }
	else if ('hot' == $type )
	{
        $where .= " AND g.is_host = 1 ";
    }


    /* 获得稿件列表 */
    $sql = 'SELECT g.goods_id, g.goods_name,g.cat_id,g.cat2_id,g.act_id, g.goods_name_style, g.market_price, g.is_new, g.is_best, g.is_hot, g.shop_price AS org_price, ' ."IFNULL(mp.user_price, g.shop_price * '$_SESSION[discount]') AS shop_price, g.promote_price, g.goods_type, " .
                'g.promote_start_date, g.promote_end_date, g.goods_brief, g.goods_thumb , g.goods_img ' .
            'FROM ' . $GLOBALS['ecs']->table('goods') . ' AS g ' .
            'LEFT JOIN ' . $GLOBALS['ecs']->table('member_price') . ' AS mp ' .
                "ON mp.goods_id = g.goods_id AND mp.user_rank = '$_SESSION[user_rank]' " .
            "WHERE $where ORDER BY g.sort_order, g.last_update DESC ";
    $res = $GLOBALS['db']->selectLimit($sql, 10 , 1);
    $arr = array();
    while ($row = $GLOBALS['db']->fetchRow($res))
    {
        if ($row['promote_price'] > 0)
        {
            $promote_price = bargain_price($row['promote_price'], $row['promote_start_date'], $row['promote_end_date']);
        }
        else
        {
            $promote_price = 0;
        }

        /* 处理商品水印图片 */
        $watermark_img = '';

        if ($promote_price != 0)
        {
            $watermark_img = "watermark_promote_small";
        }
        elseif ($row['is_new'] != 0)
        {
            $watermark_img = "watermark_new_small";
        }
        elseif ($row['is_best'] != 0)
        {
            $watermark_img = "watermark_best_small";
        }
        elseif ($row['is_hot'] != 0)
        {
            $watermark_img = 'watermark_hot_small';
        }

        if ($watermark_img != '')
        {
            $arr[$row['goods_id']]['watermark_img'] =  $watermark_img;
        }

        $arr[$row['goods_id']]['goods_id']         = $row['goods_id'];
        if($display == 'grid')
        {
            $arr[$row['goods_id']]['goods_name']       = $GLOBALS['_CFG']['goods_name_length'] > 0 ? sub_str($row['goods_name'], $GLOBALS['_CFG']['goods_name_length']) : $row['goods_name'];
        }
        else
        {
            $arr[$row['goods_id']]['goods_name']       = $row['goods_name'];
        }
        $arr[$row['goods_id']]['name']             = $row['goods_name'];
		$arr[$row['goods_id']]['cat_id']           = $row['cat_id'];
		$arr[$row['goods_id']]['cat2_id']          = $row['cat2_id'];
		$arr[$row['goods_id']]['act_id']           = $row['act_id'];
        $arr[$row['goods_id']]['goods_brief']      = $row['goods_brief'];
        $arr[$row['goods_id']]['goods_style_name'] = add_style($row['goods_name'],$row['goods_name_style']);
        $arr[$row['goods_id']]['market_price']     = price_format($row['market_price']);
        $arr[$row['goods_id']]['shop_price']       = price_format($row['shop_price']);
        $arr[$row['goods_id']]['type']             = $row['type'];
        $arr[$row['goods_id']]['promote_price']    = ($promote_price > 0) ? price_format($promote_price) : '';
        $arr[$row['goods_id']]['goods_thumb']      = get_image_path($row['goods_id'], $row['goods_thumb'], true);
        $arr[$row['goods_id']]['goods_img']        = get_image_path($row['goods_id'], $row['goods_img']);
        $arr[$row['goods_id']]['url']              = 'goods2.php?id='.$row['goods_id'];
		$arr[$row['goods_id']]['cat_name']		   = get_cat_name($row['cat_id']);
		$arr[$row['goods_id']]['short_name']	   = $GLOBALS['_CFG']['goods_name_length'] > 0 ?
                                               sub_str($row['goods_name'], $GLOBALS['_CFG']['goods_name_length']) : $row['goods_name'];

    }

    return $arr;
}

?>